﻿Shader "Dan/UI/UI_LineTravel"
{
    Properties
    {
       _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
        _MainTex ("MainTex", 2D) = "white" {}
        _FadeOut ("FadeOut", Range(0, 1)) = 0
        _Outside_Width ("Outside_Width", Float ) = 0.2
        _Speed_R ("Speed_R", Float ) = 1
        _Inside_Width ("Inside_Width", Float ) = 0.1
        _Speed_G ("Speed_G", Float ) = 1
        _Grid_X ("Grid_X", Float ) = 4
        _Grid_Y ("Grid_Y", Float ) = 4
        _Grid_Width ("Grid_Width", Range(0, 1)) = 0.1
        _GridOpacity ("GridOpacity", Range(0, 1)) = 0.5
        _ShinnyAmount ("ShinnyAmount", Float ) = 1
        _IconShakeSpeed ("IconShakeSpeed", Float ) = 1
        _IconShakeStrength ("IconShakeStrength", Float ) = 1
    }
    SubShader
    {
        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100

        Pass
        {
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
               
                float4 vertex : SV_POSITION;
            };

            float4 _Color;
            sampler2D _MainTex;  
            float4 _MainTex_ST;
            float _FadeOut;
            float _Speed_R;
            float _Speed_G;
            float _Grid_X;
            float _Grid_Y;
            float _ShinnyAmount;
            float _GridOpacity;
            float _IconShakeSpeed;
            float _Inside_Width;
            float _Outside_Width;
            float _Grid_Width;
            float _IconShakeStrength;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                
                return o;
            }

            float DrawNet (float2 uv , float2 netUV , float width){
              netUV = frac(netUV*uv)*2-1;
              float net = saturate(max(abs(netUV.x),abs(netUV.y)));
              return step(1.0 - width,net);
             }

            
            float ShakeRD (float shakeSpeed){
                float2 shakeGrad = float2(shakeSpeed,shakeSpeed);
                float2 sk = shakeGrad + 0.2127+shakeSpeed*0.3713*shakeSpeed;
                float2 rnd = 4.789*sin(489.123*(sk));
                return frac(rnd.x*rnd.y*(1+sk.x));
            } 

            void ShakeUV (float2 uv ,float shakeSpeed,float shakeStrength, float shinnyAmount,out float2 noiseUV,out float shinny){
                float shakeSpeed01 = _Time.x*shakeSpeed*0.05;//定义两种震动时间
                float shakeSpeed02 = _Time.x*shakeSpeed*0.08;
                float shakeX = ShakeRD(shakeSpeed01);//计算两种随机震动
                float shakeY = ShakeRD(shakeSpeed02);
                noiseUV = frac(float2(shakeX,shakeY)*0.005*shakeStrength+uv);//UV震动
                shinny = (clamp((shakeX+shakeY),0,0.5)*shinnyAmount)+1;//计算明暗
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv = i.uv;
                float3 finalColor = _Color.rgb;
                float4 time = _Time; 
                uv =TRANSFORM_TEX(uv, _MainTex);
                
                
                float2 tempUV =  (uv - _FadeOut*0.5) / (1-_FadeOut);
                //计算外圈
                float outerMask = tex2D(_MainTex,tempUV).a;
                float outWidth = abs(_Outside_Width)*0.15;
                float2 outLineUV =  tempUV  * (outWidth*2+1)-outWidth;
                float innerMask = tex2D(_MainTex,outLineUV).a;
                float outerRing = outerMask-innerMask;
                //计算内圈
                float2 uvOffset = tempUV*1.1-0.05;
                float midMask = tex2D(_MainTex,uvOffset).a;
                float innerWidth = (abs(_Inside_Width)+0.6)*0.1;
                float2 innerUV =  tempUV  * (innerWidth*2+1)-innerWidth ;
                float centerMask = tex2D(_MainTex,innerUV).a;
                float innerRing = midMask-centerMask;

                //求极坐标
                float2 polarUV = tempUV*2.0-1.0;
                float polar = atan2(polarUV.x,polarUV.y)/6.28318530718+0.5;
                //逆时针CD
                float rotLeft = frac(polar+(time.y*_Speed_R)*0.5);
                //采样R通道贴图
                float rotationR = tex2D(_MainTex,float2(rotLeft,1)).r;
                //顺时针CD
                float rotRight = frac(polar+(time.y*_Speed_G)*-0.35);
                //采样G通道贴图
                float rotationG = tex2D(_MainTex,float2(rotRight,1)).g;
                
                //计算震动的图标
                float2 noiseUV;
                float shinny;
                ShakeUV(uv,_IconShakeSpeed,_IconShakeStrength,_ShinnyAmount,noiseUV,shinny);
                float iconShape = tex2D(_MainTex,noiseUV).b;
                float shakeIcon =iconShape*shinny*midMask;
                 
                float shape01 =outerRing*rotationR;// 逆时针旋转的外圈
                float shape02 =rotationG*innerRing;// 顺时针旋转的内圈
                float fade =saturate(_FadeOut*-4+1);//衰减值
                
                float gridNet =DrawNet(uv,float2(_Grid_X,_Grid_Y),_Grid_Width);//画网格
                gridNet = clamp(gridNet,0.3,1);//限制网格透明度 
                gridNet*= centerMask*(1.0 - iconShape)*_GridOpacity;//网格叠加遮罩叠加图标和透明度

                float alpha =shape01+shape02+shakeIcon+gridNet;//网格和内圈 外圈 图标相加
                
                return fixed4(finalColor,alpha*fade);//填入透明通道*衰减
            }
            ENDCG
        }
    }
}
